package com.qtu.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域请求配置类
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.setAllowCredentials(true);//允许客户端发送包含 cookie的用户凭证
        corsConfiguration.addAllowedHeader("*");//允许所有请求头
        corsConfiguration.addAllowedOrigin("*");//允许跨域
        corsConfiguration.addAllowedMethod("*");//允许所有请求方式

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
